//
//  ConsultationsController.m
//  Cabinet
//
//  Created by Florent Montsegur on 24/04/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConsultationsController.h"
#import "Person.h"

@implementation ConsultationsController

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        mConsultations = [[NSMutableArray alloc] init];
        
    }
    
    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView{
    return [mConsultations count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    
    if( rowIndex >= [mConsultations count])
        return @"";
    
    Consultation *cons = [mConsultations objectAtIndex:rowIndex];
    
    if( [[aTableColumn identifier] caseInsensitiveCompare:@"date"] == NSOrderedSame ){
        return [[cons CreationDate] descriptionWithCalendarFormat:@"%d/%m/%y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
    }
    
    if( [[aTableColumn identifier] caseInsensitiveCompare:@"diag"] == NSOrderedSame )
        return [cons Diagnostic];    
               
    return @"";
}

- (void) setConsultations:(NSArray*)aConsultList{
    [self emptyConsultaions];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"CreationDate" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    
    [mConsultations addObjectsFromArray:[aConsultList sortedArrayUsingDescriptors:sortDescriptors]];
    
}

- (Consultation*)getConsultationAtIndex:(NSInteger)aRowIndex{
    if(aRowIndex<0) 
         return nil;
    
    if(aRowIndex>=[mConsultations count]) 
        return nil;
    
    return [mConsultations objectAtIndex:aRowIndex];
    
}

- (void) emptyConsultaions {
    [mConsultations removeAllObjects];
}

- (void)dealloc
{    
    [self emptyConsultaions];
}

@end
